package com.onekbase.demo.thead;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;

public class CountDownLatchDemo2 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(3);
        for (int num = 0; num < 3; num++){
            new Thread(() ->{
                try {
                    Thread.sleep(1000+ ThreadLocalRandom.current().nextInt(1000));
                    System.out.println(Thread.currentThread().getName() + "finish task");

                    countDownLatch.countDown();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }

        countDownLatch.await();
        System.out.println("所有线程执行完成,对结果进行汇总");
    }
}
